package cn.edu.hzw.comic.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import cn.edu.hzw.comic.R
import cn.edu.hzw.comic.mvp.model.Chapter
import cn.edu.hzw.comic.mvp.model.Page
import kotlinx.android.synthetic.main.item_chapter.view.*

class ChapterAdapter(var data:List<Chapter> = ArrayList<Chapter>(),val itemClick:(View,Int)->Unit)
    : RecyclerView.Adapter<ChapterAdapter.PageViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_chapter,parent,false)
        return  PageViewHolder(itemView)
    }

    fun refreshData(newData:List<Chapter>){
        data = newData
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
        bindView(holder.itemView,position)
    }

    private fun bindView(itemView: View, position: Int) {
        val page = data[position]
        itemView.tvTitle.text = page.title
        itemView.tvTitle.setOnClickListener {
            itemClick(itemView,position)
        }
    }

    override fun getItemCount(): Int = data.size

    class PageViewHolder(itemView: View):RecyclerView.ViewHolder(itemView)
}